package com.tarena.lbs.basic.service.impl;

import com.tarena.lbs.basic.dao.repository.AreaRepository;
import com.tarena.lbs.basic.pojo.po.AreaPO;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import com.tarena.lbs.basic.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaRepository areaRepository;

    @Override
    public List<AreaVO> queryTree() {
        //1.查询所有省
        List<AreaPO> provinces = areaRepository.queryProvinces();
        //
        List<AreaVO> vos = new ArrayList<>();
        for(AreaPO po:provinces){
            AreaVO vo = new AreaVO();
            vo.setId(po.getId());
            vo.setParentId(po.getParentId());
            vo.setName(po.getName());
            vo.setChildren(getChild(po.getId()));
            vos.add(vo);
        }

        return vos;
    }

    @Override
    public List<AreaVO> queryProvinces() {
        List<AreaPO> provinces = areaRepository.queryProvinces();
        List<AreaVO> vos = new ArrayList<>();
        for(AreaPO po:provinces){
            AreaVO vo = new AreaVO();
            vo.setId(po.getId());
            vo.setParentId(po.getParentId());
            vo.setName(po.getName());
            vo.setChildren(getChildren(po.getId()));
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<AreaVO> getChildren(Integer parentId) {
        List<AreaPO> child = areaRepository.queryChildren(parentId);
        List<AreaVO> vos = new ArrayList<>();
        for(AreaPO po:child){
            AreaVO vo = new AreaVO();
            vo.setId(po.getId());
            vo.setParentId(po.getParentId());
            vo.setName(po.getName());
            vo.setChildren(getChildren(po.getId()));
            vos.add(vo);
        }
        return vos;
    }

    //查询下一级城市
    private List<AreaVO> getChildren(Long id) {
        List<AreaPO> child = areaRepository.queryChild(id);
        List<AreaVO> vos = new ArrayList<>();
        for(AreaPO po:child){
            AreaVO vo = new AreaVO();
            vo.setId(po.getId());
            vo.setParentId(po.getParentId());
            vo.setName(po.getName());
        }
        return vos;
    }

    //递归查询下属所有市区
    private List<AreaVO> getChild(Long id) {
        List<AreaPO> child = areaRepository.queryChild(id);
        List<AreaVO> vos = new ArrayList<>();
        for(AreaPO po:child){
            AreaVO vo = new AreaVO();
            vo.setId(po.getId());
            vo.setParentId(po.getParentId());
            vo.setName(po.getName());
            if(po.getDepth() != 3) {
                vo.setChildren(getChild(po.getId()));
            }else {

                vo.setChildren(null);
            }
            vos.add(vo);
        }

        return vos;
    }
}






















